package com.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.dto.PageQuery;
import com.common.dto.RankDTO;
import com.common.entity.Developer;
import com.common.util.DeveloperConverter;
import com.common.vo.DeveloperVO;
import com.common.vo.PageResult;
import com.web.mapper.DeveloperMapper;
import com.web.service.DeveloperService;
import com.web.service.FormatDeveloper;
import com.web.service.RankModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author nomo
 * @Date 2024/10/25 15:57
 */
@Service
@Slf4j
public class DeveloperServiceImpl extends ServiceImpl<DeveloperMapper,Developer> implements DeveloperService{

    @Autowired
    private DeveloperMapper developerMapper;

    @Autowired
    private FormatDeveloper formatDeveloperService;

    @Autowired
    private RankModel rankModelService;

    public PageResult pageQuery(PageQuery pageQuery) {
        Page<Developer> page = new Page<>(pageQuery.getPage(), pageQuery.getPageSize(), true);
        Page<Developer> developerPage = developerMapper.selectPage(page, new QueryWrapper<>());

        // 获取总条数
        long total = developerPage.getTotal();

        // 将数据库数据转换为VO，使用转换器，并按 percentile 降序排序
        List<DeveloperVO> collect = developerPage.getRecords().stream()
                .map(DeveloperConverter::convertToVO)
                .sorted((vo1, vo2) -> Double.compare(vo2.getPercentile(), vo1.getPercentile())) // 按 percentile 降序排序
                .collect(Collectors.toList());

        PageResult pageResult = new PageResult(total, collect);
        return pageResult;
    }


    //从数据库里查询一个开发者信息
    public DeveloperVO getDeveloper(String userName) {

        //在数据库中查找,有则返回，没有则插入再返回
        LambdaQueryWrapper<Developer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Developer::getUserName, userName);
        Developer developer1 = developerMapper.selectOne(queryWrapper);
        System.out.println(developer1);
        Developer developer = developer1;
        if (developer != null) {
            String location = developer.getLocation();
            //预测location
            if(location == null){
                developer.setLocation("未知");
            }
            //转换器
            return DeveloperConverter.convertToVO(developer);
        }

        //用名字获取对应数据，并计算插入数据库
        DeveloperVO developerVO = formatDeveloperService.handle(userName);

        if(developerVO == null){
            return null;
        }

        String location = developerVO.getLocation();
        //预测location
        if(location == null){
            developerVO.setLocation("未知");
        }

        return developerVO;
    }

    //查询指定地区的数据
    public PageResult query(String areas) {
        // 创建 QueryWrapper 对象
        QueryWrapper<Developer> queryWrapper = new QueryWrapper<>();

        // 添加模糊查询条件
        queryWrapper.like("location", areas);

        // 执行查询
        List<Developer> developers = developerMapper.selectList(queryWrapper);

        // 将查询结果转换为 DeveloperVO 列表
        List<DeveloperVO> developerVOS = developers.stream()
                .map(developer -> {
                    DeveloperVO developerVO = new DeveloperVO();
                    BeanUtils.copyProperties(developer, developerVO);
                    return developerVO;
                })
                .sorted((vo1, vo2) -> Double.compare(vo2.getPercentile(), vo1.getPercentile())) // 按 percentile 降序排序
                .collect(Collectors.toList());

        return new PageResult(developerVOS.size(), developerVOS);
    }

    public void  cleanData(){
        // 1. 查询所有 Developer 数据
        List<Developer> developers = developerMapper.selectList(new QueryWrapper<>());  // 查询所有数据

        // 2. 遍历数据并修改 percentile 和 level
        for (Developer developer : developers) {

            RankDTO rankDTO = rankModelService.calculateRank(developer);

            developer.setPercentile(rankDTO.getPercentile());
            developer.setLevel(rankDTO.getLevel());
            // 3. 更新每一条记录
            developerMapper.updateById(developer);
        }

        System.out.println("数据清洗结束");

    }
}


